package com.example.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ruiji.common.BaseContext;
import com.example.ruiji.common.CustomException;
import com.example.ruiji.pojo.R;
import com.example.ruiji.pojo.ShoppingCart;
import com.example.ruiji.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")


public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dish=shoppingCart.getDishId();
        //用用户ID拿出对应数据库 查看是新添加还是继续添加
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dish!=null)
        {
            //添加的是商品
            queryWrapper.eq(ShoppingCart::getDishId,dish);

        }else
        {
            //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,dish);
        }
        //根据ID查询当前商品是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        //判断是否是重复商品
        if(one!=null)
        {
            //重复商品  拿出数量加1即可
            Integer number=one.getNumber();
            number=number+1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }else
        {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }

        return R.success(one);

    }

@GetMapping("/list")

    public R<List<ShoppingCart>> list()
{
    //根据ID查询对应购物车数据即可
    Long currentId = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,currentId);
    List<ShoppingCart> list = shoppingCartService.list(queryWrapper);


    return R.success(list);
}

@PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart)
{
   //取消购物车 两种状态  一种是减少次数 一种是删除
   //1.拿出用户ID
    Long currentId = BaseContext.getCurrentId();
    //2.判断一下是套餐还是商品
    Long id=shoppingCart.getDishId();
    LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(ShoppingCart::getUserId,currentId);
    if(id!=null)
    {
        queryWrapper.eq(ShoppingCart::getDishId,id);
        //要删减的商品
    }else
    {
        queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        //删减的是套餐
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        int size = list.size();
        ShoppingCart shoppingCart1=list.get(0);
        Long ida=shoppingCart1.getId();
        shoppingCartService.removeById(ida);
         return  R.success("套餐删除成功");
    }
    //拿
    ShoppingCart one = shoppingCartService.getOne(queryWrapper);
    if(one!=null)
    {
        //拿出数量
        Integer number = one.getNumber();
        if(number>=2)
        {
            number=number-1;
            one.setNumber(number);
            shoppingCartService.updateById(one);
        }else
        {
            //number只能是1 这个时候 需要从数据库删除
           shoppingCartService.removeById(one);
        }

    }else
    {
        return R.error("数据库中无数据");
    }

    return R.success("成功");
}
//全部清空
@DeleteMapping("clean")
    public  R<String> delet()
    {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);
        return R.success("成功");
    }



}


